package org.qloudgen.borg.pages.panel;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.AjaxLink;
import org.apache.wicket.ajax.markup.html.form.AjaxSubmitLink;
import org.apache.wicket.extensions.ajax.markup.html.modal.ModalWindow;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.Radio;
import org.apache.wicket.markup.html.form.RadioGroup;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.PageableListView;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.Model;
import org.qloudgen.borg.CommonValues;
import org.qloudgen.borg.beans.FuncBean;
import org.qloudgen.borg.beans.ParamBean;
import org.qloudgen.borg.pages.model.FuncParamModel;

@SuppressWarnings({ "unchecked", "rawtypes" })
public class AppFuncParameterPanel extends Panel {
        
	private static final long serialVersionUID = 1L;
	private transient FuncParamModel fpm;
	
    public AppFuncParameterPanel(String id , final FuncBean func)
    {
        super(id);
		
		Form<?> form = new Form("form",new CompoundPropertyModel<FuncParamModel>(new FuncParamModel())) {
			
			private static final long serialVersionUID = 1L;

			@Override
			protected void onSubmit() {
				
				fpm = (FuncParamModel)getDefaultModelObject();
					
				if (fpm.getGroup() == null){
					
					info(CommonValues.INFO_NO_SELECT);
					return;
					
				} 
			}
		};
		this.add(form);
		
		final RadioGroup<?> group=new RadioGroup<Object>("group"); 
		group.setOutputMarkupId(true);
		group.setRenderBodyOnly(false);
		form.add(group);
		
		final PageableListView<ParamBean> listView =new PageableListView<ParamBean>( "rows", func.getParams(), CommonValues.SHOW_LIST_NUMBER) {

			private static final long serialVersionUID = 1L;

			@Override
			protected void populateItem(ListItem<ParamBean> item) {
				final ParamBean param = (ParamBean) item.getModelObject();
				item.add(new Radio<ParamBean>("radio", new Model<ParamBean>(param)));
				item.add( new Label( "paramCode", param.getParamCode()));
				item.add( new Label( "paramName", param.getParamName()));
				item.add( new Label( "paramDesc", param.getParamDesc()));
				item.add( new Label( "paramType", CommonValues.paramTypes.get(param.getParamType())));
				item.add( new Label( "paramInfo", param.getParamInfo()));					
				
			}
		};
		listView.setOutputMarkupId(true);
		group.add(listView);

        // window
		final ModalWindow modal = new ModalWindow("modal");
        this.add(modal);

        modal.setContent(new AddFuncParameterPanel(modal.getContentId(), func , modal));
        modal.setTitle("添加功能参数");
        modal.setCookieName("modal");

        modal.setCloseButtonCallback(new ModalWindow.CloseButtonCallback()
        {
			private static final long serialVersionUID = 1L;

			@Override
            public boolean onCloseButtonClicked(AjaxRequestTarget target)
            {
                return true;
            }
        });

        modal.setWindowClosedCallback(new ModalWindow.WindowClosedCallback()
        {
			private static final long serialVersionUID = 1L;

			@Override
            public void onClose(AjaxRequestTarget target)
            {
                target.add(group);
            }
        });

		// AjaxLink add 
		form.add(new AjaxLink<Void>("add"){

			private static final long serialVersionUID = 1L;

			@Override
			public void onClick(AjaxRequestTarget target) {
				
				modal.show(target);
			}});
		
		// AjaxButton delete
		form.add(new AjaxSubmitLink("delete"){
			
			private static final long serialVersionUID = 1L;
			protected void onAfterSubmit(AjaxRequestTarget target, Form<?> form) {
				
				if (fpm == null || fpm.getGroup() == null) return;
				for (int i = 0; i<func.getParams().size(); i++) {
					if (func.getParams().get(i).getParamCode().equals(fpm.getGroup().getParamCode())){
						func.getParams().remove(i);
						break;
					}
				}
				target.add(group);
				
			}});
    }

}
